/*!
 * jQuery ExpandSelect AutoComplete Plugin
 * version: 1.0
 * @requires jQuery v1.6.4 or later
 *
 * Examples and documentation at: https://code.google.com/p/expand-select-auto-complete/
 * licensed under the MIT:
 *   http://www.opensource.org/licenses/mit-license.php
 */
(function($){
	ExpandSelect_AutoComplete = function(options) {
	   
		var defaults = {
			select_id: '',
			text_id: '',
			top: 20
		};
		var options = $.extend(defaults, options);
		$("#"+options.select_id).bind("click change keyup",function(){
				 $("#"+options.text_id).val($(this).val());
				 $("#"+options.text_id).trigger('change');
		});
		//var s_default = $("#" + options.text_id).val();
		$("#" + options.text_id).bind('keyup', function(){
			//if ($(this).val() != s_default) {
				//s_default = $(this).val();
				var supplierValue;
				var supplierValueLength;
				var spidText;
				var i;
				try {
					supplierValue = $(this).val();
					supplierValueLength = supplierValue.length;
					var is_match = false;
					$("#" + options.select_id).children().each(function(){
						spidText = $(this).val();
						spidText = spidText.substring(0, supplierValueLength);
						if (supplierValue == spidText) {
							$(this).attr("selected", "selected");
							is_match = true;
							return false;
						}
					});
					if(!is_match){
						$("#" + options.select_id + " > option").eq(0).attr("selected","selected");
					}
					ExpandSelect(options.select_id, options.text_id, options.top, is_match);
				} 
				catch (e) {
					alert(e);
				}
			//}
		});
	};
})(jQuery);

function ExpandSelect(select,text,top,is_match)
{
	$("#"+select).hide();
	var select_val = $("#"+select+" > option:selected").val();
	if($("#"+select+"_ExpandSelect").length){
		if(!$("#"+select+"_ExpandSelect").is(":visible")){
			$("#"+select+"_ExpandSelect").show();
		}
		 $("#"+select+"_ExpandSelect").children().each(function(){
			if($(this).val() == select_val){
				$(this).removeAttr("selected");
				$(this).attr("selected","selected");
				return false;
			}
		 });
	}else{
		var max_length;
		$("#"+select+' option').each(function(){
		  max_length = Math.max(max_length, $(this).val().length);
		});
		var ExpandSelect = "<select id='"+select+"_ExpandSelect' class='ExpandSelect' size='10' style='position:relative; margin-top:" + top + "px; z-index:1000000; " + (max_length > 200 ? 'width:'+max_length+'px;' : '') + ";'>";
		var select_val = $("#" + select+" > option:selected").val();
		var select_key = 0;
		$("#" + select).children().each(function(key,value){
			if($(this).val() == select_val){
				select_key = key;
			}
			ExpandSelect += "<option value='" + $(this).val() + "'>" + $(this).val() + "</option>"; 
		});
		ExpandSelect += "</select>";
		$("#"+select).before(ExpandSelect);
		$("#"+select+"_ExpandSelect").show();
		$("#" + select + "_ExpandSelect > option").eq(select_key).attr("selected","selected");
		$("#"+select+"_ExpandSelect > option").live({
			mouseover: function(){
				$(this).css("background","#316ac5");
			},
			mouseout: function(){
				$(this).css("background","white");
			}
		});
		$("body").click(function(event){
			if($(event.target).attr("id") != select+"_ExpandSelect"){
				$("#"+select+"_ExpandSelect").hide();
				$("#"+select).show();
			}
		});
		$("#"+select+"_ExpandSelect").bind("click change",function(){
			var E_select_val = $("#"+select+"_ExpandSelect > option:selected").val();
			$("#"+select).children().each(function(){
				if($(this).val() == E_select_val){
					$(this).attr("selected","selected");
					return false;
				}
			});
			$("#"+text).val($("#"+select+"_ExpandSelect > option:selected").val());
			$("#"+text).trigger("change");
			$("#"+select+"_ExpandSelect").hide();
			$("#"+select).show();
		});
	}
	if(!is_match){
		$("#"+select+"_ExpandSelect").hide();
		$("#"+select).show();
	}
}